﻿using System;
using System.Collections.Generic;
using System.Linq;

using Guoc.BigMall.Infrastructure.Extension;
using Guoc.BigMall.Domain;
using Guoc.BigMall.Domain.ValueObject;
using Guoc.BigMall.Infrastructure;
using Guoc.BigMall.Infrastructure.Utils;
using System.ComponentModel;

namespace Guoc.BigMall.Domain.Entity
{
    public class SaleOrder : BaseEntity
    {
        public SaleOrder()
        {
            this.Items = new List<SaleOrderItem>();
            this.CreatedOn = DateTime.Now;
            this.UpdatedOn = DateTime.Now;
            this.OrderType = 1;
            this.Status = SaleOrderStatus.WaitToPay;
            this.PaymentWay = ValueObject.PaymentWay.WechatPay;
            this.PayStatus = OrderPayStatus.Unpaid;
        }
        /// <summary>
        /// 订单编号 ： 单据类型 2 + 账号4位+ （日期20160101） 3位+ 下单的秒数（86400，5位）+ 2位随机
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 门店
        /// </summary>
        public int StoreId { get; set; }
       
        /// <summary>
        /// 订单类型：销售单1，销售退单2
        /// </summary>
        public int OrderType { get; set; }
        /// <summary>
        /// 支付方式
        /// </summary>
        public PaymentWay? PaymentWay { get; set; }
       
         /// <summary>
         ///  付款状态
         /// </summary>
         public OrderPayStatus PayStatus { get; set; }
        /// <summary>
        /// 支付日期
        /// </summary>
        public DateTime? PaidDate { get; set; }

        public int Hour { get; set; }

        /// <summary>
        /// 订单金额 = 实际价格RealAmount * 数量
        /// </summary> 
        public decimal OrderAmount { get; set; }        
        /// <summary>
        /// 销售单状态
        /// </summary>
        public SaleOrderStatus Status { get; set; }

        public DateTime CreatedOn { get; set; }

        /// <summary>
        ///  下单客户
        /// </summary>
        public int MemberId { get; set; }

        /// <summary>
        ///  推会员ID
        /// </summary>
        public int ReferMemberId { get; set; }

        public int UpdatedBy { get; set; }

        public DateTime UpdatedOn { get; set; }       

        public string ReceiverAreaId { get; set; }
        public string ReceiverAreaFullName { get; set; }
        /// <summary>
        /// 收货人地址
        /// </summary>
        public string ReceiverAddress { get; set; }
        /// <summary>
        ///  收货人联系电话
        /// </summary>
        public string ReceiverTel { get; set; }
        /// <summary>
        ///  收货人姓名
        /// </summary>
        public string ReceiverName { get; set; }


        public virtual List<SaleOrderItem> Items { get; set; }

        public string Remark { get; set; }

        

        /// <summary>
        ///  汇总订单金额，不包括优惠金额
        /// </summary>
        /// <returns></returns>
        public decimal SumOrderAmount()
        {
            if (this.Items.Count == 0) { throw new FriendlyException("订单明细为空"); }
            var totalAmount= this.Items.Sum(n => n.GetRowTotal());
            this.OrderAmount = totalAmount;
            // 这里可以减去折扣金额

            return totalAmount;
        }

        /// <summary>
        /// 完成支付
        /// </summary>
        public void FinishPiad()
        {
            if (this.Status != SaleOrderStatus.WaitToPay) {
                { throw new FriendlyException("订单状态必须为待支付"); }
            }
            this.Status = SaleOrderStatus.Paid;
            this.PayStatus = OrderPayStatus.Paid;
            this.PaidDate = DateTime.Now;
            this.UpdatedOn = DateTime.Now;
            this.UpdatedBy = 1;  // 系统管理员
        }

        /// <summary>
        ///  添加收货地址
        /// </summary>
        /// <param name="mbrAddress"></param>
        public void AddMemberAddress(MemberAddress mbrAddress)
        {
            this.ReceiverName = mbrAddress.Name;
            this.ReceiverTel = mbrAddress.Tel;
            this.ReceiverAddress = mbrAddress.Address;
            this.ReceiverAreaId = mbrAddress.AreaId;
            this.ReceiverAreaFullName = mbrAddress.AreaFullName;
        }

    }


    public enum OrderPayStatus
    {
        [Description("未付款")]
        Unpaid = 1,
        [Description("已付款")]
        Paid = 2,
        [Description("付款后取消")]
        CanceledAfterPaid
    }

}